﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	public partial class EditPipeTypeForm : System.Windows.Forms.Form
	{
		public EditPipeTypeForm()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
		}

		public EditPipeTypeForm(Autodesk.Revit.DB.Document doc, Element elem)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.doc = doc;
			if (elem is PipeType)
			{
				this.isPipe = true;
				this.pipeType = (elem as PipeType);
				this.oldTypeName = this.pipeType.Name;
				this.hytbTypeName.Text = this.pipeType.Name;
				return;
			}
			if (elem is DuctType)
			{
				this.isPipe = false;
				this.ductType = (elem as DuctType);
				this.oldTypeName = this.ductType.Name;
				this.hytbTypeName.Text = this.ductType.Name;
			}
		}

		public bool JudgeNameContainTeShu(string name)
		{
			bool result = false;
			if (name.Contains("\\") || name.Contains(":") || name.Contains("{") || name.Contains("}") || name.Contains("[") || name.Contains("]") || name.Contains("|") || name.Contains(";") || name.Contains("<") || name.Contains(">") || name.Contains("?") || name.Contains("`") || name.Contains("~"))
			{
				result = true;
			}
			return result;
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			string text = this.hytbTypeName.Text.Trim();
			if (string.IsNullOrEmpty(text) || this.JudgeNameContainTeShu(text))
			{
				MessageBox.Show("类型名称不能为空,并且不能包含下列任一字符：\\:{}[]|;<>?`~！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.hytbTypeName.Focus();
				return;
			}
			if (this.IsTypeNameExist(text))
			{
				MessageBox.Show("类型名称已存在", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.hytbTypeName.Focus();
				return;
			}
			if (this.oldTypeName == text)
			{
				base.DialogResult = DialogResult.Cancel;
			}
			this.pipeTypeName = this.hytbTypeName.Text.Trim();
			base.DialogResult = DialogResult.OK;
		}

		private bool IsTypeNameExist(string name)
		{
			bool result = false;
			List<string> list = new List<string>();
			if (this.isPipe)
			{
				list = YJKElementFilter.GetElementNameByClass(typeof(PipeType), this.doc);
			}
			else
			{
				list = YJKElementFilter.GetElementNameByClass(typeof(DuctType), this.doc);
			}
			list.Remove(this.oldTypeName);
			using (List<string>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current == name)
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private Document doc;

		private PipeType pipeType;

		private DuctType ductType;

		public string pipeTypeName = "";

		private string oldTypeName = "";

		private bool isPipe;
	}
}
